/**
 * 
 */
package com.sig.models;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.List;
import java.util.Vector;


/**
 * @author zou
 *
 */
public class Segment{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/**
	 * begin Segment
	 */
	private Point p1;
	
	
	/**
	 * End Segment
	 */
	private Point p2;
	
	/**
     * Vertex buffer.
     */
    private FloatBuffer vertexBuffer;

   
    /**
     * Index buffer.
     */
    private ShortBuffer indexBuffer;
    
    /**
     * byte buffer
     */
    private ByteBuffer bbuffer;
    
    /**
     * lageur 
     */
    private float roadsize=0.00004f/2f;
    
    /**
     * lenght between two point
     */
    float normVector;
    float angle;
    /**
     * Constructor
     * @param p1
     * @param p2
     */
	public Segment(Point p1, Point p2) {
		super();
		this.p1 = p1;
		this.p2 = p2;
		//this.vectorLength(p1, p2);
		this.manageVertexBuffer(vectorLength(p1, p2));
		this.manageIndexBuffer();
		
	}
	
	
	
	
	/**
	 * @return the p1
	 */
	public Point getP1() {
		return p1;
	}




	/**
	 * @return the p2
	 */
	public Point getP2() {
		return p2;
	}




	/**
	 * @return the vertexBuffer
	 */
	public FloatBuffer getVertexBuffer() {
		return vertexBuffer;
	}




	/**
	 * @return the indexBuffer
	 */
	public ShortBuffer getIndexBuffer() {
		return indexBuffer;
	}

	/**
	 * @return the angle
	 */
	public float getAngle() {
		return angle;
	}

	/**
	 * 
	 * @param p1  			
	 * @param p2
	 * @return
	 * 			lenght of vector
	 */
	private float vectorLength(Point p1, Point p2){
		float dx=(float)p2.getX()-p1.getX();
		float dy=(float)p2.getY()-p1.getY();
		angle = (float) ((Math.atan2(dy, dx) * 180f) / Math.PI);
		normVector=(float) Math.sqrt(dx*dx+dy*dy);
		
		return normVector;
	}
	
	/**
	 * create vertex buffer
	 */
	private void manageVertexBuffer(float normVector){
		bbuffer = ByteBuffer.allocateDirect(48);
		bbuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = bbuffer.asFloatBuffer();
        vertexBuffer.put(new float[] {0, roadsize, 0, 
                        			  normVector, roadsize, 0, 
                                      0, -roadsize, 0, 
                                      normVector, -roadsize, 0 });
        vertexBuffer.position(0);
	}
	
	
	/**
	 * create index buffer
	 */
	private void manageIndexBuffer(){
		bbuffer = ByteBuffer.allocateDirect(12);
		bbuffer.order(ByteOrder.nativeOrder());
        indexBuffer = bbuffer.asShortBuffer();
        indexBuffer.put(new short[] { 0, 1, 2, 1, 2, 3 });
        indexBuffer.position(0);
	}

	
	
}
